package mpc;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;

public class DrawingArea extends JPanel implements MouseListener, MouseMotionListener{
	
	private VirtualField vfield;
	final int MAZE_WIDTH = 10, MAZE_HEIGHT = 10, 
			SQ_SIZE = 50, DIST = 2, MODE_X = MAZE_WIDTH * (SQ_SIZE + DIST + 1), MODE_W = 150, MODE_H = 100,
			dX = -8, dY = -35;
			//dx, dy;
	
	private int currentMode;
	
	private Mode[] mode;
	
	DrawingArea(){
		vfield = new VirtualField(MAZE_WIDTH, MAZE_HEIGHT);
		
		mode = new Mode[3];
		mode[0] = new Mode(Mode.STARTING, "Set Start", MODE_X, 0 * MODE_H, MODE_W,	MODE_H, false);
		mode[1] = new Mode(Mode.BRICKING, "Manage Bricks", MODE_X, 1 * MODE_H, MODE_W,	MODE_H, false);
		mode[2] = new Mode(Mode.STEPPING, "Step", MODE_X, 2 * MODE_H, MODE_W,	MODE_H, false);
		
		//dx = this.getX();
		//dy = this.getY();
	}
	
	private Color chooseColorForState(int state){
		if(state == VirtualField.INF) return Color.yellow;
		if(state == VirtualField.START) return Color.green;
		if(state == VirtualField.BRICK) return Color.darkGray;
		if(state == VirtualField.IN_QUEUE) return Color.magenta;
		if(state == VirtualField.FIRST_IN_QUEUE) return Color.blue;
		if(state == VirtualField.WORKED) return Color.cyan;
		
		return null;
	}
	private void checkMode(int x, int y){
		x += dX; y += dY;
		for(int i = 0; i < mode.length; i++){
			if(mode[i].onMe(x, y)){
				currentMode = mode[i].mode;
				for(int j = 0; j < mode.length; j++)
					mode[j].enabled = false;
				mode[i].enabled = true;
				this.repaint();
				return;
			}
		}
	}
	private XYPair findBox(int x, int y){
		x += dX; y += dY;
		//this.addMouseListener(this);
		if(!((x > 0) && (x < MAZE_WIDTH * (SQ_SIZE + DIST)) && (y > 0) && (y < MAZE_HEIGHT * (SQ_SIZE + DIST)))) return new XYPair(-1, -1);
		return new XYPair(x / (SQ_SIZE + DIST), y / (SQ_SIZE + DIST));
		
	}
	
	@Override 
	public void paintComponent(Graphics g) {
        //super.paintComponent(g);  // Ask parent to paint background.

            // user resized window.
		g.setColor(Color.black);
		g.fillRect(0, 0, MAZE_WIDTH * (SQ_SIZE + DIST), MAZE_HEIGHT * (SQ_SIZE + DIST));
		//g.setColor(Color.yellow);
		for(int i = 0; i < MAZE_WIDTH; i++){
			for(int j = 0; j < MAZE_HEIGHT; j++){
				/*if(vfield.getState(i, j) == VirtualField.INF) g.setColor(Color.yellow);
				else if(vfield.getState(i, j) == 0) g.setColor(Color.green);
				else if(vfield.getState(i, j) == VirtualField.BRICK) g.setColor(Color.DARK_GRAY);
				else 
					*/
				
				g.setColor(chooseColorForState(vfield.getState(i, j)));
				
				
				g.fillRect(i * (SQ_SIZE + DIST), j * (SQ_SIZE + DIST), SQ_SIZE, SQ_SIZE);
				
				g.setColor(Color.black);
				g.drawString(""+vfield.getDist(i, j), i * (SQ_SIZE + DIST) + 10, j * (SQ_SIZE + DIST) + 20);
			}
		}
		
		for(int i = 0; i < mode.length; i++){
			mode[i].draw(g);
		}
		/*
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, 800, 800);
		g.setColor(Color.BLUE);
        g.fillOval(0, 0, 800, 800);
        */
    }

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
		checkMode(e.getX(), e.getY());
		
		if(currentMode == Mode.BRICKING){
			XYPair b = findBox(e.getX(), e.getY());
			if(b.x == -1) return;
			vfield.setBrick(b.x, b.y);
		}
		
		if(currentMode == Mode.STARTING){
			XYPair b = findBox(e.getX(), e.getY());
			if(b.x == -1) return;
			vfield.setStart(b.x, b.y);
		}
		
		if(currentMode == Mode.STEPPING){
			vfield.doNextStep();
		}
		
		
		this.repaint();
		//System.out.println(b.x + " " + b.y + " " + e.getX() + " " + e.getY());
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
